package com.ybb.servlet;

import com.ybb.pojo.User;
import com.ybb.service.LoginService;
import com.ybb.service.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userCode=req.getParameter("userCode");
        String userPassword=req.getParameter("userPassword");
        LoginService loginService=new LoginServiceImpl();
        User user=loginService.login(userCode,userPassword);
        if(user==null){
            req.getSession().setAttribute("error","账号或密码输入错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else{
            req.getSession().setAttribute("userSession", user);
             req.getRequestDispatcher("jsp/frame.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
